package com.oepm.web.controller;

import com.oepm.biz.TitleVerifyBiz;
import com.oepm.common.entity.Meta;
import com.oepm.common.entity.Result;
import com.oepm.common.utils.JWTUtil;
import com.oepm.dao.entity.EstabProjectPo;
import com.oepm.dao.entity.QueryInfo;
import com.oepm.dao.entity.TeacherDemoApplicationPo;
import com.oepm.dao.entity.TitleVerifyInfoVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@CrossOrigin//解决跨域
@RestController
@RequestMapping("/titleVerify")
@RequiresRoles(logical = Logical.OR,value = {"1","2","3"})
public class TitleVerifyController {
    @Autowired
    TitleVerifyBiz titleVerifyBiz;

    /**
     * 立项审核列表
     * @param request
     * @return
     */
    @RequiresRoles(logical = Logical.OR,value = {"1","2","3"})
    @GetMapping("/select")
    Result select(HttpServletRequest request, QueryInfo queryInfo){
        String token=request.getHeader("Authorization");
        //获取学院
        String academy = JWTUtil.getAcademy(token);
        //获取角色id
        String roleid = JWTUtil.getRoleId(token);
        Map<String,Object> map = new HashMap<>();
        map.put("roleid",roleid);
        if(roleid.equals("1")){
            map.put("data",titleVerifyBiz.adminTitle(queryInfo));
        }else if (roleid.equals("2")){
            map.put("data",titleVerifyBiz.labTitle(academy,queryInfo));
        }else if (roleid.equals("3")){
            map.put("data",titleVerifyBiz.academyTitle(academy,queryInfo));
        }
        Result result = new Result(Meta.SUCCESS,map);
        return result;
    }

    /**
     * 立项审核
     * @param info
     * @return
     */
    @PostMapping("/update")
    @RequiresRoles(logical = Logical.OR,value = {"1","2","3"})
    Result verify(@RequestBody TitleVerifyInfoVo info){
        int flag = 0;
        flag = titleVerifyBiz.verify(info);
        Result result;
        if (flag>0){
            result = new Result(Meta.SUCCESS);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }

    /**
     * 审核通过后插入立项表
     * @param ePP
     * @return
     */
    @PostMapping("/insert")
    Result uploadEstablishTitle(@RequestBody EstabProjectPo ePP){
        //重点项目规则，NKSZ+年份+四位数编号
        //普通项目规则，NKSP+年份+四位数编号
        int id = ePP.getId();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        if (ePP.getLevel().equals("普通")){
            if (id/10==0){
                ePP.setPid("NKSP"+year+"000"+id);
            }else if(id/100==0){
                ePP.setPid("NKSP"+year+"00"+id);
            }else if(id/1000==0){
                ePP.setPid("NKSP"+year+"0"+id);
            }else if(id/10000==0){
                ePP.setPid("NKSP"+year+id);
            }
        }else if (ePP.getLevel().equals("重点")){
            if (id/10==0){
                ePP.setPid("NKSZ"+year+"000"+id);
            }else if(id/100==0){
                ePP.setPid("NKSZ"+year+"00"+id);
            }else if(id/1000==0){
                ePP.setPid("NKSZ"+year+"0"+id);
            }else if(id/10000==0){
                ePP.setPid("NKSZ"+year+id);
            }
        }
        int flag,flag1,flag2,flag3;
        String pid = ePP.getPid();
        flag = titleVerifyBiz.uploadEstablishTitle(ePP);//添加数据
        flag1 = titleVerifyBiz.uploadScore(pid);
        flag2 = titleVerifyBiz.uploadResult(pid);
        flag3 = titleVerifyBiz.uploadApp(pid);
        Result result;
        if (flag>0 & flag1>0 & flag2>0 & flag3>0){
            result = new Result(Meta.SUCCESS);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }

    @GetMapping("/selectId/{id}")
    Result selectById(@PathVariable String id){
        TeacherDemoApplicationPo teaPo = titleVerifyBiz.selectById(id);
        ArrayList<TeacherDemoApplicationPo> list = new ArrayList<>();
        list.add(teaPo);
        Result result = new Result(Meta.SUCCESS, list);
        return result;
    }
}
